<?php
/**
 * Created by PhpStorm.
 * User: zd
 * Date: 2018/12/5
 * Time: 16:00
 */

namespace app\enterprise\controller;


use app\enterprise\controller\BaseController;
use \app\enterprise\service\Message as MessageService;
use think\Cookie;
use think\Request;
use think\Session;

class Message extends BaseController
{
    public  $token = '';

    public function _initialize()
    {
        parent::_initialize();
        $this->token = Cookie::get('Token');
        $this->assign('token',$this->token);
    }
        /**
     * 根据用户id获取行为
     * @param $token
     * @return \think\response\Json
     */
        public function getMessage(){
            $server = new MessageService();
            $list =  $server->getMessage($this->token);
            if (empty($list)){
                $data=['data'=>''];
            }else{
                $data=['data'=>$list];
            }
            return json($data);
        }

    /**
     * 到聊天列表页面
     * @return mixed
     */
        public function gotoMessageList(){
            $data = Request::instance()->param();
            if ($data) {
                $worker = new MessageService();
                $workerModel = $worker->getWorkerUserInfo($this->token);
                $user = $worker->getUserInfo($this->token,$data['user_id']);

                $userInfo['id'] = $user->user->id;
                $userInfo['nickname'] = $user->user->nickname;
                $userInfo['avatar_url'] = $user->user->avatar_url;

                $workerinfo['name'] = $workerModel->name;
                $workerinfo['head_img'] = $workerModel->userimg->url;
                $workerinfo['position'] = $workerModel->position;
                $workerinfo['company']= $workerModel->thirdapp->name;
                $workerinfo['img']= $workerModel->thirdapp->head_img;

                $this->assign('user',json_encode($userInfo));
                $this->assign('worker',json_encode($workerinfo));
                $this->assign('user_id',$data['user_id']);
                return $this->fetch('message/chat');
            }
        }
    /**
     * 获取洗信息列表
     * @param $token
     * @return \think\response\Json
     */
    public function getMessageList($uid,$page){
        $server = new MessageService();
        $list =  $server->getMessageList($uid,$page,$this->token);
        if (empty($list)){
            $data=['data'=>''];
        }else{
            $data=['data'=>$list];
        }
        return json($data);
    }

    /**
     * 改变消息状态
     * @param $uid
     * @param $token
     * @return \think\response\Json
     */
    public function changeMessageStatus($uid){
        $server = new MessageService();
        $result =  $server->changeMessageStatus($uid,$this->token);
        if (!$result){
            $data=['data'=>''];
        }else{
            $data=['data'=>$result];
        }
        return json($data);
    }

    /**
     * 删除联系人
     * @param $uid
     * @return \think\response\Json
     */
    public function deleteUser($uid){
        $server = new MessageService();
        $result =  $server->deleteUser($uid,$this->token);
        if (!$result){
            $data=['data'=>''];
        }else{
            $data=['data'=>$result];
        }
        return json($data);
    }

}